import { userBehavior } from './behavior'
import { fileUpload, updateUser } from '../../../../api/user'
Page({

  behaviors: [userBehavior],

  // 页面的初始数据
  data: {
    isShowPopup: false // 控制更新用户昵称的弹框显示与否
  },
  // 修改头像
  async onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { data } = await fileUpload(avatarUrl, 'file')
    this.setData({
      "userInfo.headimgurl": data
    })
  },
  async submitFn() {
    const res = await updateUser(this.data.userInfo)
    console.log(res)
    if (res.code === 200) {
      wx.toast({ title: '用户信息修改成功' })
      wx.setStorage({ key: "userInfo", data: this.data.userInfo })
      this.setUserInfo(this.data.userInfo)
      wx.navigateBack()
    }
  },
  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      'userInfo.nickname': this.data.userInfo.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false
    })
  },

  // 获取修改后的昵称
  getnickName(e) {
    const { nickname } = e.detail.value
    this.setData({
      'userInfo.nickname': nickname,
      isShowPopup: false
    })
  }

})